//
//  ViewController.m
//  VideoDemo
//
//  Created by Chen Charmin on 15/4/20.
//  Copyright (c) 2015年 Chen Charmin. All rights reserved.
//

#import "ViewController.h"
//#import "VideoViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController ()
//@property (nonatomic,strong) VideoViewController *video;
//@property(nonatomic,strong) MPMoviePlayerController *moviePlayer;
@property (nonatomic,strong) MPMoviePlayerViewController *moviePlayerViewController;
@property (nonatomic,strong) UIImageView *adImage;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(UIImageView *)adImage{
    _adImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _adImage.image = self.imageView.image;
    return  _adImage;
}
- (IBAction)playAction:(id)sender {
//    self.video = [[VideoViewController alloc] initWithNibName:@"VideoViewController" bundle:nil];
//    [self presentViewController:self.video animated:YES completion:^{
//        
//    }];
//    NSURL *url = [self getNetworkURL];
//    _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
//    _moviePlayer.controlStyle = MPMovieControlStyleDefault;
//    [_moviePlayer prepareToPlay];
//    _moviePlayer.view.frame = self.view.bounds;
//    _moviePlayer.view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
//    [self.view addSubview:_moviePlayer.view];
//    [self.moviePlayer play];
//    [self addNotification];
    self.moviePlayerViewController = nil;
    [self presentMoviePlayerViewControllerAnimated:self.moviePlayerViewController];
}

-(MPMoviePlayerViewController *)moviePlayerViewController{
    if (!_moviePlayerViewController) {
        NSURL *url = [self getNetworkURL];
        _moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
        [self addNotification];
        [self thumbnailImageRequest];
    }
    return _moviePlayerViewController;
}
-(void)thumbnailImageRequest{
    [self.moviePlayerViewController.moviePlayer requestThumbnailImagesAtTimes:@[@15.0,@30.0] timeOption:MPMovieTimeOptionNearestKeyFrame];
}

-(NSURL *)getNetworkURL{
    NSString *urlStr = [NSString stringWithFormat:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"];
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStr];
    return url;
}

-(void)addNotification{
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayerViewController.moviePlayer];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerViewController.moviePlayer];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerThumbnailRequestFinished:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:self.moviePlayerViewController.moviePlayer];
}

-(void)mediaPlayerThumbnailRequestFinished:(NSNotification *)notification{
    self.imageView.image = notification.userInfo[MPMoviePlayerThumbnailImageKey];
    NSLog(@"capture");
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, nil, nil, nil);
}

-(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{
    switch (self.moviePlayerViewController.moviePlayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"playing");
            [self.adImage removeFromSuperview];
            self.adImage = nil;
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"pause");
            break;
        case MPMoviePlaybackStateStopped:
            NSLog(@"stop");
            break;
            
        default:
            NSLog(@"state:%li",(long)self.moviePlayerViewController.moviePlayer.playbackState);
            break;
    }
}

-(void)mediaPlayerPlaybackFinished:(NSNotification *)notification{
    NSLog(@"finished. %li",(long)self.moviePlayerViewController.moviePlayer.playbackState);
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
